/**
 * nghilt
 * 2011/09/26
 *
 * Copyright by N&N.,Inc
 */
package vn.nnn.shogi.quanco;

import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList;

import javax.swing.JApplet;

import at.com.vn.chess.util.CommonMethod;
import at.com.vn.chess.util.Const;
import at.com.vn.chess.util.RectangleUtils;

import vn.nnn.shogi.board.Board;

/**
 * 
 */
public class QuanTot extends QuanCo {

	public QuanTot( Rectangle toaDo, int color, boolean normalDirection, JApplet mainApplet ) {
		super( toaDo, color, normalDirection, mainApplet );	
	}
	
	@Override
	public void setToaDoNhaTu() {
		toaDoNhaTu = new Point( 2, 1 );
	}

	@Override
	public boolean canMove( Board board, Rectangle destination,
			QuanCo foundedPiece, ArrayList<QuanCo> listQuanCo ) {
		
		boolean canmove = false;
		
		Rectangle quanThap = toaDo;
		Rectangle quanCao = destination;
		
		if( normalDirection ) {
			if( color == QUAN_DEN ) {
				quanThap = destination;
				quanCao = toaDo;
			}
		}else {
			if( color == QUAN_TRANG ) {
				quanThap = destination;
				quanCao = toaDo;
			}
		}
		
		// Neu nam tren cung mot cot
		if( RectangleUtils.isInSameColumn( quanThap, quanCao ) ) {
			if( RectangleUtils.isNextRow( quanThap, quanCao ) ) {
				canmove = true;	
			}		
		}
		
		// Dieu kien co ban khi di quan
		if ( canmove ) {
			canmove = isPassToBasicMoveCondition( board, destination, foundedPiece, listQuanCo );
		}		
		
		return canmove;
	}
	
	@Override
	public void upgrade( Rectangle destination, ArrayList<QuanCo> listQuanCo ) {
		QuanTotPhongCap totPhongCap = new QuanTotPhongCap( destination, color, normalDirection, getMainApplet() );
//		QuanXePhongCap totPhongCap = new QuanXePhongCap( destination, color, normalDirection, getMainApplet() );
		int idxChosen = getPositionInList( listQuanCo );
		
		listQuanCo.remove( idxChosen );
		listQuanCo.add( idxChosen, totPhongCap );	
	}
	
	@Override
	public boolean canRelease( Board board, Rectangle destination, ArrayList<QuanCo> listQuanCo ) {
		
		// Neu khong vuot qua duoc dieu kien tha quan co ban
		if( !isPassToBasicReleaseCondition( board, destination, listQuanCo ) ) {
			return false;
		}
		
		/* Luat han che tha quan tot thu nhat: Khong duoc tha mot quan tot tren cung mot cot 
		 * voi mot quan tot cung phe chua phong cap */
		ArrayList<QuanCo> listQuanTot = timToanBoQuanTotCungPhe( listQuanCo );
		for ( int i = 0; i < listQuanTot.size(); i++ ) {
			if( RectangleUtils.isInSameColumn( listQuanTot.get( i ).toaDo, destination ) ) {
				return false;
			}
		}
		
		// Luat han che tha quan tot thu hai: khong duoc tha o hang cuoi cung
		if( board.isInVungCamThaQuanLoai1( destination, color ) ) {
			return false;
		}
		
		// Luat han che tha quan tot thu ba: Quan tot khong duoc tha o nuoc chieu het		
		ArrayList<QuanCo> dummyList = CommonMethod.cloneListQuanCo( listQuanCo );
		QuanCo dummyQuanDuocTha = dummyList.get( getPositionInList( listQuanCo ) );
		dummyQuanDuocTha.move( board, destination, Const.UNDEFINE_VALUE, dummyList );
		
		int whichSide = CommonMethod.switchColor( color );
		if( CommonMethod.isHetCo( board, dummyList, whichSide ) ) {
			return false;
		}		 
		
		return true;
	}
	
	@Override
	public boolean isTrongVungBatBuocPhongQuan( Board board, Rectangle destination ) {
		if( board.isInVungCamThaQuanLoai1( destination, color ) ) {
			return true;
		}
		return false;
	}
	
	private ArrayList<QuanCo> timToanBoQuanTotCungPhe( ArrayList<QuanCo> listQuanCo ) {
		ArrayList<QuanCo> listQuanTot = new ArrayList<QuanCo>();
		QuanCo quanCo;
		for ( int i = 0; i < listQuanCo.size(); i++ ) {			
			quanCo = listQuanCo.get( i );
			if ( quanCo instanceof QuanTot && quanCo.color == color ) {
				listQuanTot.add( quanCo );				
			}			
		}
		return listQuanTot;
	}
}
